package com.rabbitmqconsumer.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

@Service
@RabbitListener(queues = "ConfirmQueue")// 监听确认队列
public class ConfirmReceiver {
	@RabbitHandler
	void process(Map ReceiverMessage, Message message, Channel channel) throws IOException {
		try {
			System.out.println("消费消息："+ ReceiverMessage);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
			System.out.println("消费消息确认："+message.getMessageProperties().getConsumerQueue()+"，接收到了回调方法");
		}catch (Exception e){
			channel.basicNack(message.getMessageProperties().getDeliveryTag(),false, true);
			System.out.println("尝试重发："+message.getMessageProperties().getConsumerQueue());
			channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
		}
	}
}
